;
(function (window, angular) {
    'use strict';
    angular.module("Controller.Report.OrderSatisfyRate", [])
        .controller("OrderSatisfyRateCtrl", ["$scope", "$state", "ybWidget", "ybHttp", "MESG_TYPE", "$filter", "$timeout", "UserContextService", function ($scope, $state, ybWidget, ybHttp, MESG_TYPE, $filter, $timeout, UserContextService) {
            // 订单满足率分页信息
            $scope.pageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            // 采购组分页信息
            $scope.purchasePageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            // 供货商分页信息
            $scope.supplierPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            // 零件类型分页信息
            $scope.materialPageInfo = {
                totalPages: 1,
                totalRows: 0,
                pageSize: 10,
                curPage: 1
            };

            // 标签页信息
            $scope.activeFlag = [true, false, false, false];

            $scope.mainTb = {
                scrollWidth: 2000,
                tableCol: [
                    {
                        name: "采购订单号",
                        dataField: "billNo",
                        width: 160,
                        isShow: true
                    },
                    {
                        name: "供货商名称",
                        dataField: "supplierOrgName",
                        width: 150,
                        isShow: true
                    },
                    {
                        name: "供货商代码",
                        dataField: "supplierCode",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "订单下达时间",
                        dataField: "submitDateStr",
                        width: 150,
                        isShow: true
                    },
                    {
                        name: "采购员",
                        dataField: "purchaseGroup",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "零件编号",
                        dataField: "materialsNo",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "零件中文名",
                        dataField: "materialsDes",
                        width: 120,
                        isShow: true
                    },
                    {
                        name: "零件分类",
                        dataField: "materialsType",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "单价",
                        dataField: "settlementPrice",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "订单数量",
                        dataField: "qty",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "订单金额",
                        dataField: "amount",
                        width: 100,
                        isShow: true
                    },
                    {
                        name: "最迟交期",
                        dataField: "lastDlvDateStr",
                        width: 150,
                        isShow: true
                    },
                    {
                        name: "交货期内实际到货数量",
                        dataField: "periodQty",
                        width: 150,
                        isShow: true,
                    },
                    {
                        name: "交货期内实际到货金额",
                        dataField: "periodAmount",
                        width: 150,
                        isShow: true,
                    },
                    {
                        name: "实际交期",
                        dataField: "lastDateStr",
                        width: 150,
                        isShow: true,
                    },
                    {
                        name: "订单完成率",
                        dataField: "orderRate",
                        width: 100,
                        isShow: true,
                    }
                ]
            };

            $scope.purchaseTb = {
                scrollWidth: 450,
                tableCol: [
                    {
                        name: "采购员",
                        dataField: "purchaseGroup",
                        width: 150,
                        isShow: true
                    },
                    {
                        name: "行数",
                        dataField: "count",
                        width: 150,
                        isShow: true,
                    },
                    {
                        name: "订单满足率",
                        dataField: "orderRate",
                        width: 150,
                        isShow: true,
                    }
                ]
            };

            $scope.supplierTb = {
                scrollWidth: 450,
                tableCol: [
                    {
                        name: "供货商名称",
                        dataField: "supplierOrgName",
                        width: 150,
                        isShow: true
                    },
                    {
                        name: "行数",
                        dataField: "count",
                        width: 150,
                        isShow: true,
                    },
                    {
                        name: "订单满足率",
                        dataField: "orderRate",
                        width: 150,
                        isShow: true,
                    }
                ]
            };

            $scope.materialTb = {
                scrollWidth: 450,
                tableCol: [
                    {
                        name: "零件分类",
                        dataField: "materialsType",
                        width: 150,
                        isShow: true
                    },
                    {
                        name: "行数",
                        dataField: "count",
                        width: 150,
                        isShow: true,
                    },
                    {
                        name: "订单满足率",
                        dataField: "orderRate",
                        width: 150,
                        isShow: true,
                    }
                ]
            };

            $scope.exportParam = {
                mainUrl: ybHttp.RequestUrl("device", "/report/exportOrderSatisfyRate"),
                purchaseUrl: ybHttp.RequestUrl("device", "/report/exportOrderSatisfyRatePurchaseGroup"),
                supplierUrl: ybHttp.RequestUrl("device", "/report/exportOrderSatisfyRateSupplierName"),
                materialUrl: ybHttp.RequestUrl("device", "/report/exportOrderSatisfyRateMaterialsType"),
            };

            // 搜索条件
            $scope.condition = {};
            $scope.purchaseCondition = {};
            $scope.supplierCondition = {};
            $scope.materialCondition = {};

            $scope.dataList = [];
            $scope.purchaseList = [];
            $scope.supplierList = [];
            $scope.materialList = [];

            $scope.toolbarConfig = {
                showAdvanceSearch: false,
                showColConfig: false
            };

            $scope.datePickerFlag = [false, false, false, false];

            $scope.reset = function (obj) {
                $scope[obj] = {};
            };

            $scope.switchToolbar = function (toolbar) {
                if (!!$scope.toolbarConfig["show" + toolbar]) {
                    $scope.toolbarConfig["show" + toolbar] = false;
                } else {
                    $timeout(function () {
                        $scope.toolbarConfig["show" + toolbar] = true;
                    });
                }
            };

            $scope.switchDataPicker = function (index) {
                $scope.datePickerFlag = [false, false, false, false];
                if (angular.isDefined(index)) {
                    $scope.datePickerFlag[index] = true;
                }
            };

            $scope.checkDate = function (obj, startFiled, endFiled) {
                var timeFormatObj = ybWidget.CheckDate(obj[startFiled], obj[endFiled]);
                if (!!timeFormatObj.tStart) {
                    obj[startFiled] = timeFormatObj.tStart;
                }
                if (!!timeFormatObj.tEnd) {
                    obj[endFiled] = timeFormatObj.tEnd;
                }
            };

            // 按钮权限code
            var btnPermissionCode = {
                purchaseSatisfyRateExport: window.ActionCode.purchaseSatisfyRateExport,
                supplierSatisfyRateExport: window.ActionCode.supplierSatisfyRateExport,
                materialSatisfyRateExport: window.ActionCode.materialSatisfyRateExport,
                satisfyRateExport: window.ActionCode.satisfyRateExport,
            };

            // 按钮权限
            $scope.btnPermission = {
                purchaseSatisfyRateExport: true,
                supplierSatisfyRateExport: true,
                materialSatisfyRateExport: true,
                satisfyRateExport: true,
            };

            // 设置按钮权限
            function setBtnPermission() {
                for (var key in btnPermissionCode) {
                    $scope.btnPermission[key] = UserContextService.CheckPermission([btnPermissionCode[key]]);
                }
            }

            $scope.changeTab = function (index) {
                angular.forEach($scope.activeFlag, function (item) {
                    item = false;
                });
                $scope.activeFlag[index] = true;
            };

            $scope.searchDataList = function (curPage) {
                getDataList(curPage);
            };

            $scope.searchPurchaseList = function (curPage) {
                getPurchaseList(curPage);
            };

            $scope.searchSupplierList = function (curPage) {
                getSupplierList(curPage);
            };

            $scope.searchMaterialList = function (curPage) {
                getMaterialList(curPage);
            };

            // 获取调度员报表列表
            function getDataList (curPage) {
                var url = '/report/orderSatisfyRate';
                if (curPage) $scope.pageInfo.curPage = curPage;
                removeNullKey($scope.condition);
                var params = {
                    filters: angular.copy($scope.condition),
                    pageSize: $scope.pageInfo.pageSize,
                    pageNum: $scope.pageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    angular.forEach(res.value.list, function (item) {
                        item.lastDateStr = $filter("YbDate")(item.lastDate, "yyyy-MM-dd HH:mm:ss");
                        item.lastDlvDateStr = $filter("YbDate")(item.lastDlvDate, "yyyy-MM-dd HH:mm:ss");
                        item.submitDateStr = $filter("YbDate")(item.submitDate, "yyyy-MM-dd HH:mm:ss");
                    });
                    $scope.pageInfo.totalRows = res.value.total;
                    $scope.pageInfo.totalPages = res.value.pages;
                    $scope.dataList = res.value.list;
                })
            }

            // 获取代理商报表列表
            function getPurchaseList (curPage) {
                var url = '/report/orderSatisfyRatePurchaseGroup';
                if (curPage) $scope.purchasePageInfo.curPage = curPage;
                removeNullKey($scope.purchaseCondition);
                var params = {
                    filters: angular.copy($scope.purchaseCondition),
                    pageSize: $scope.purchasePageInfo.pageSize,
                    pageNum: $scope.purchasePageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.purchasePageInfo.totalRows = res.value.total;
                    $scope.purchasePageInfo.totalPages = res.value.pages;
                    $scope.purchaseList = res.value.list;
                })
            }

            // 获取订单类型报表列表
            function getSupplierList (curPage) {
                var url = '/report/orderSatisfyRateSupplierName';
                if (curPage) $scope.supplierPageInfo.curPage = curPage;
                removeNullKey($scope.supplierCondition);
                var params = {
                    filters: angular.copy($scope.supplierCondition),
                    pageSize: $scope.supplierPageInfo.pageSize,
                    pageNum: $scope.supplierPageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.supplierPageInfo.totalRows = res.value.total;
                    $scope.supplierPageInfo.totalPages = res.value.pages;
                    $scope.supplierList = res.value.list;
                })
            }

            // 获取采购组报表列表
            function getMaterialList (curPage) {
                var url = '/report/orderSatisfyRateMaterialsType';
                if (curPage) $scope.materialPageInfo.curPage = curPage;
                removeNullKey($scope.materialCondition);
                var params = {
                    filters: angular.copy($scope.materialCondition),
                    pageSize: $scope.materialPageInfo.pageSize,
                    pageNum: $scope.materialPageInfo.curPage
                };
                ybHttp.Post('device', url, params).then(function (res) {
                    if (res.code !== 0) {
                        ybWidget.ShowMessage(res, MESG_TYPE.Error);
                        return;
                    }
                    $scope.materialPageInfo.totalRows = res.value.total;
                    $scope.materialPageInfo.totalPages = res.value.pages;
                    $scope.materialList = res.value.list;
                })
            }

            // 删除对象中空的值
            function removeNullKey (obj) {
                Object.keys(obj).forEach(function (key) {
                    if (!obj[key] && obj[key] !== 0) delete obj[key];
                });
            }

            (function () {
                UserContextService.SetBtnPermission(setBtnPermission);
            })()
        }])
    ;
})(window, window.angular);